<template>

  <div>
    <el-table v-loading="loading" :data="datas">
      <el-table-column prop="course_title" label="课程" />
      <el-table-column prop="create_time" label="学完时间" />
      <el-table-column
        fixed="right"
        label="操作"
        width="100"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="printCert(scope)">证书下载</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row v-if="total > 0" type="flex" justify="end" class="pagination">
      <el-pagination
        :current-page.sync="paginate.page"
        :page-size="paginate.count"
        :page-sizes="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </el-row>
    <el-dialog v-if="printVisible" title="证书打印" :visible.sync="printVisible" width="860px" top="4vh" :close-on-click-modal="false">
      <print-page :cert="cert" :direction="direction" />
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="change">横纵切换</el-button>
        <el-button v-print="printObj" type="primary" size="small">打印</el-button>
      </span>
    </el-dialog>
  </div>

</template>
<script>
import PrintPage from '@/pages/course/cert/components/print.vue';
import site from '@/site.js'
import Vue from 'vue';
import Print from 'vue-print-nb';
Vue.use(Print);

export default {
  components: {
    PrintPage
  },
  inject: ['reload'],
  data() {
    return {
      uri: '/user/admin/student/' + this.$route.params.id + '/cert/list',
      response: null,
      loading: true,
      paginate: {
        count: 10,
        page: 1
      },
      pageSize: site.page_sizes,
      printVisible: false,
      printObj: {
        id: 'print',
        popTitle: '证书打印'
      },
      cert: {
        cert_info: {},
        course_list: []
      },
      direction: 'vertical'
    };
  },
  computed: {
    datas() {
      if (this.response) {
        return this.response.data;
      }
      return [];
    },
    total() {
      if (this.response) {
        return this.response.total;
      }
      return 0;
    }
  },
  created() {
    this.loadData();
  },
  methods: {
    handleCurrentChange: function(page) {
      this.paginate.page = page;
      this.loadData();
    },
    loadData: function() {
      this.$http.get(
          this.uri,
          {
            ...this.paginate
          },
          ({ data }) => {
            const {
              datas,
              paginate
            } = data
            this.response = {
              data: datas,
              ...paginate
            }
            this.loading = false;
          },
          _ => {
            this.loading = false;
          },
          false
      );
    },
    printCert: function({ row }) {
      // 获取证书信息
      this.$http.get(
        '/user/admin/student/cert/print',
        {
          id: row.id,
          user_id: row.user_id,
          course_id: row.course_id
        },
        response => {
          this.cert = response.data;
          this.printVisible = true;
        },
        _ => {
          this.$message.error('获取证书信息失败');
        }
      );
    },
    change: function() {
      this.direction = this.direction == 'vertical' ? 'horizontal' : 'vertical';
    }
  }
};
</script>
<style scoped>
@media print {
  body {
    -webkit-print-color-adjust: exact;
    -moz-print-color-adjust: exact;
    -ms-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  button {
    display: none;
  }
}

.pagination{
  padding: 20px 0;
}
</style>
